import * as echarts from 'echarts'

// 增加了自定义名称设置
export function initPieChart (domId, chartData = [], customName = '默认名称') {
  var chartDom = document.getElementById(domId)
  if (!chartDom) return
  var myChart = echarts.init(chartDom)

  // 新增行，计算总和
  let total = chartData.reduce((acc, curr) => acc + curr.value, 0)

  var option = {
    tooltip: {
      trigger: 'item'
    },
    legend: {
      top: '5%',
      left: 'center'
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,

        // 这段原有的注释掉
        /* label: {
          show: false,
          position: 'center'
        }, */
        /* emphasis: {
          label: {
            show: true, // 改成false试试？
            fontSize: 40,
            fontWeight: 'bold'
          }
        }, */
        labelLine: {
          show: false
        },
        data: chartData,

        // 新增，设置中心标签
        label: {
          normal: {
            show: true,
            position: 'center',
            formatter: `${total}\n${customName}`, // {b} 是系列名, {c} 是自定义文本
            textStyle: {
              fontSize: 20,
              fontWeight: 'bold'
            }
          }
        }
      }
    ]
  }

  myChart.setOption(option)
  return myChart
}
